package com.codeplex.springsurv.businessLogic;

import com.codeplex.springsurv.*;
import com.codeplex.springsurv.dataaccess.mappers.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseStatus;

public class UserService {

	@Resource(name = "userMapper")
	private UsersMapper userMapper;
	
	
	//Checks if a user given is registered and if the password matches with the stored one for that user.
	public Boolean getAuthUser(User pUser){
		Boolean aux = false;
		User usr = null;
		try {
			usr = this.userMapper.getUserByNickAndPass(pUser.getStrNick(), pUser.getStrPass());
			if(usr != null){
				if(usr.getStrNick() != null){
					aux = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return aux;
	}
	
	//registers a new user in the system.
	public void insertNewUser(User pUser){
		try{
			if(!existsUser(pUser.getStrNick())){
				this.userMapper.insertUser(pUser);
			}
		}catch (SQLException e){
			e.printStackTrace();
		}
	}
	
	public Boolean existsUser(String pNick){
		Boolean exists = false;
		User usr = null;
		try {
			usr = this.userMapper.existsNick(pNick);
			if(usr != null){
				if(usr.getStrNick() != null){
					exists = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return exists;
	}
}
